import apiClient from '@/api/interceptors';

/**
 * 获取集群列表
 */
export async function getClusterList() {
    try {
        const response = await apiClient.get('/clusters/list');
        if (response.data.code === 1) {
            return response.data.data;
        } else {
            throw new Error(response.data.msg || '获取集群列表失败');
        }
    } catch (error) {
        console.error('请求集群列表失败:', error);
        throw error;
    }
}

/**
 * 创建集群
 */
export async function createCluster(clusterData) {
    try {
        const response = await apiClient.post('/clusters', clusterData);
        if (response.data.code === 1) {
            return response.data.data;
        } else {
            throw new Error(response.data.msg || '创建集群失败');
        }
    } catch (error) {
        console.error('创建集群失败:', error);
        throw error;
    }
}


/**
 * 更新集群
 */
export async function updateCluster(clusterId, clusterData) {
    try {
        const response = await apiClient.post('/clusters/update', clusterData, {
            params: { clusterId }
        });
        if (response.data.code === 1) {
            return response.data.data;
        } else {
            throw new Error(response.data.msg || '更新集群失败');
        }
    } catch (error) {
        console.error('更新集群失败:', error);
        throw error;
    }
}

/**
 * 删除集群
 */
export async function deleteCluster(clusterId) {
    try {
        const response = await apiClient.delete(`/clusters`, {
            params: { clusterId }
        });
        if (response.data.code === 1) {
            return response.data.data;
        } else {
            throw new Error(response.data.msg || '删除集群失败');
        }
    } catch (error) {
        console.error('删除集群失败:', error);
        throw error;
    }
}